package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"shuqi_novel/models/mysql"

	"shuqi_novel/novelrpc/internal/svc"
	"shuqi_novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelPerfectLogic {
	return &NovelPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelPerfectLogic) NovelPerfect(in *novelrpc.NovelPerfectRequest) (*novelrpc.NovelPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var findNovel mysql.Novel
	mysql.DB.Debug().Where("id", in.NovelId).Find(&findNovel)
	if findNovel.ID == 0 {
		return nil, fmt.Errorf("当前小说未存在，请确认后重新输入")
	}
	novel := mysql.Novel{
		Model:   gorm.Model{ID: uint(in.NovelId)},
		Title:   in.Title,
		Author:  in.Author,
		Content: in.Content,
		Image:   in.Image,
		UserId:  int(in.UserId),
	}
	mysql.DB.Debug().Updates(&novel)

	return &novelrpc.NovelPerfectResponse{
		NovelId: int64(novel.ID),
	}, nil
}
